package uicontrols 
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import ru.gosoplata.model.TmpModel;
	import scr.ScreenController;
	
	/**
	 * ...
	 * @author a.orleansky
	 */
	public class ButtonBase extends Sprite 
	{
		private var title:String;
		private var color:uint;
		public static var ON_PRESS_UI_BUTTON:String = "ON_PRESS_UI_BUTTON";
		public static var ON_PIC_LOADED:String = "ON_PIC_LOADED";
		//private var pic:Sprite;
		public var image:Bitmap;
		private var image_up:Bitmap = new Bitmap();
		private var image_down:Bitmap=new Bitmap();
		private var icon_up:Bitmap;
		private var icon_down:Bitmap;
		private var icon_url:String = null;
		public var provider_name:String = "error";
		
		public function ButtonBase(_title:String,_color:uint=0x101010,_iconurl:String="") 
		{
			title = _title;
			color = _color;
			name = title;
			icon_url = _iconurl;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			icon_up = new Bitmap();
			icon_down = new Bitmap();
			image = new Bitmap();
			icon_up.name = "iconup";
			icon_down.name = "icondown";
			load_pic();
			addEventListener(MouseEvent.CLICK, onPressBtn);
			
		}
		
		//private function onMouseOut(e:MouseEvent):void 
		//{
			//image_down.visible = false;
		//}
		
		//private function onMouseOver(e:MouseEvent):void 
		//{
			//image_down.visible = true;
		//}
		
		//private function drawBtn():void {
			//var sh:Shape = new Shape();
			//sh.graphics.beginFill(color);
			//sh.graphics.lineStyle(1, 0x000000);
			//sh.graphics.drawRoundRect(0, 0, 100, 50, 5, 5);
			//sh.graphics.endFill();
			//sh.x = 10;
			//sh.y = 10;
			//addChild(sh);
			//var txt:TextField = new TextField();
			//txt.text = title;
			//txt.x = 30;
			//txt.y = 10;
			//txt.selectable = false;
			//addChild(txt);
			//dispatchEvent(new Event(ON_PIC_LOADED, true));
		//}
		
		private function load_pic():void {
			var url:String;
			
			switch( name) {
				case "skip_pay": url = TmpModel.IMG_FOLDER+"/buttons/btn_skip_pay.png";
				break;
				case "back": url = TmpModel.IMG_FOLDER+"/buttons/but_back.png";
				break;
				case "exit": url = TmpModel.IMG_FOLDER+"/seach/images/search_input_exit.png";
				break;
				case "search": url = TmpModel.IMG_FOLDER+"/seach/images/search_input_but.png";
				break;
				case "menu": url = TmpModel.IMG_FOLDER+"/buttons/but_menu.png";
				break;
				case "next": url = TmpModel.IMG_FOLDER+"/buttons/but_next.png";
				break;
				case "pay": url = TmpModel.IMG_FOLDER+"/vnesenie_deneznih_sredstv/images/but_oplata.png";
				break;
				case "pay1": url = TmpModel.IMG_FOLDER+"/vnesenie_deneznih_sredstv/images/but_oplata_odnoy.png";
				break;
				//case "pay2": url = TmpModel.IMG_FOLDER+"/vnesenie_deneznih_sredstv/images/but_oplata_dve.png";
				//break;
				case "index_up_left": url = TmpModel.IMG_FOLDER+"/first_page/images/but_blue_left.png";
				break;
				case "index_up_right": url = TmpModel.IMG_FOLDER+"/first_page/images/but_blue_right.png";
				break;
				case "index_down_left": url = TmpModel.IMG_FOLDER+"/first_page/images/but_red_left.png";
				break;
				case "index_down_right": url = TmpModel.IMG_FOLDER+"/first_page/images/but_red_right.png";
				break;
				case "servise_up_left": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_left_top.png";
				break;
				case "servise_down_left": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_left_bottom.png";
				break;
				case "servise_up_right": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_right_top.png";
				break;
				case "servise_down_right": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_right_bottom.png";
				break;
				case "change_left": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_left_top.png";
				break;
				case "change_right": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_right_top.png";
				break;
				case "exchange_left": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_left_top.png";
				break;
				case "exchange_right": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_right_top.png";
				break;
				case "exchange_center": url = TmpModel.IMG_FOLDER+"/oplata_uslug/images/but_right_top.png";
				break;
				case "checkableplane": url = TmpModel.IMG_FOLDER+"/service/check_plane.png";
				break;
				case "index_info": url = TmpModel.IMG_FOLDER + "/first_page/images/ifo_but.png";
				break;
				case "search_prov": url = TmpModel.IMG_FOLDER+"/sotovie_operatori/seach/images/seach_but.png";
				break;
				case "nic_ru" : url = TmpModel.IMG_FOLDER+"/buttons/but_nic.png";
				break;
				case "but_page" : url = TmpModel.IMG_FOLDER+"/dog_oferti/images/but_num.png";
				break;
				case "but_oferta" : url = TmpModel.IMG_FOLDER+"/dog_oferti/images/but_oferta.png";
				break;
				case "but_terminal" : url = TmpModel.IMG_FOLDER+"/dog_oferti/images/but_oferta.png";
				break;
				case "confirm_excode" : url = TmpModel.IMG_FOLDER+"/sdacha/but_right_sdacha.png";
				break;
				case "wcservice" : url = TmpModel.IMG_FOLDER+"/service/btn_service.png";
				break;
				case "wcl" : url = TmpModel.IMG_FOLDER+"/service/service_wcl.png";
				break;
				case "wcr" : url = TmpModel.IMG_FOLDER+"/service/service_wcr.png";
				break;
				case "wcsend" : url = TmpModel.IMG_FOLDER+"/service/service_send.png";
				break;
				case "service_ok" : url = TmpModel.IMG_FOLDER+"/service/service_enter.png";
				break;
				default : url = TmpModel.IMG_FOLDER+"/buttons/but_operator.png";
			}
			if(name.indexOf("chplane")>-1) url = TmpModel.IMG_FOLDER+"/service/check_plane.png";
			var req:URLRequest = new URLRequest(url);
			var loader:Loader = new Loader();
			
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplite);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
			loader.load(req);
			var url2:String;
			if(name!="index_info"&&name!="wcservice"){
				url2 = url.substr(0, url.length - 4) + "_down.png";
			}else {
				url2 = url;
			}	
				var req2:URLRequest = new URLRequest(url2);
				var loader2:Loader = new Loader();
			
				
				loader2.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadCompliteDown);
				loader2.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onError);
				loader2.load(req2);
			if (icon_url.length > 2) addIcon(icon_url);
		}
		
		private function onIOError(e:IOErrorEvent):void 
		{
			trace("ButtonBase IOError");
		}
		
		private function onError(e:IOErrorEvent):void 
		{
			trace(e);
		}
		
		private var UPloaded:Boolean = false;
		private var DOWNloaded:Boolean = false;
		
		private function onLoadCompliteDown(e:Event):void 
		{
			DOWNloaded = true;
			var ldr:Loader = Loader(e.target.loader);
			image_down = Bitmap(ldr.content);
			image_down.visible = false;
			image_down.smoothing = true;
			addChild(image_down);
			setChildIndex(image_down, 0);
			if (UPloaded) {
				DOWNloaded = false;
				dispatchEvent(new Event(ON_PIC_LOADED, true));
			}
		}
		
		private function onLoadComplite(e:Event):void 
		{
			UPloaded = true;
			var ldr:Loader = Loader(e.target.loader);
			image_up = Bitmap(ldr.content);
			image_up.smoothing = true;
			addChild(image_up);
			setChildIndex(image_up, 0);
			if (DOWNloaded) {
				UPloaded = false;
				dispatchEvent(new Event(ON_PIC_LOADED, true));
			}
		}
		
		public function onPressBtn(e:Event):void 
		{
			if ((name != "menu") || !(TmpModel.payment.operation && TmpModel.payment.operation.length>5))
			{
				image_up.visible = false;
				image_down.visible = true;
				switchIconToDown(true);
				timer = 0;
				addEventListener(Event.ENTER_FRAME, showDownBtn);
			}
		}
		public var timer:Number;
		private function showDownBtn(e:Event):void 
		{
			timer++;
			if (timer == 5) {
				//if(name == "index_info" || !TmpModel.isBlockBtns())dispatchEvent(new Event(ON_PRESS_UI_BUTTON,true));
				dispatchEvent(new Event(ON_PRESS_UI_BUTTON,true));
			}
			if (timer == 10 && !swFixed) {
				image_down.visible = false;
				image_up.visible = true;
				switchIconToDown(false);
				removeEventListener(Event.ENTER_FRAME, showDownBtn);
			}
		}
		
		public var swFixed:Boolean = false;
		
		public function fixDownBtn():void {
			swFixed = true;
			image_up.visible = false;
			image_down.visible = true;
			switchIconToDown(true);
			removeEventListener(MouseEvent.CLICK, onPressBtn);
		}
		
		public function blockBtn():void {
			removeEventListener(MouseEvent.CLICK, onPressBtn);
		}
		
		
		public function unfixBtn():void {
			swFixed = false;
			image_up.visible = true;
			image_down.visible = false;
			switchIconToDown(false);
			addEventListener(MouseEvent.CLICK, onPressBtn);
		}
		
		private var needicon:Boolean = true;
		public var caption_txt:TextField;
		private var swtxticon:Boolean = false;
		public var btn_index:Number;
		
		public function addTextIcon(txt:String,size:Number = 52,_x:Number = 22,_y:Number = 5):void { 
			swtxticon = true;
			var txt_field:TextField = new TextField();
			txt_field.embedFonts = true;
			txt_field.defaultTextFormat = TmpModel.getTextFormat(size, 0xFFFFFF,true);
			txt_field.x = _x;
			txt_field.y = _y;
			txt_field.text = txt;
			txt_field.selectable = false;
			addChild(txt_field);
		}
		
		public function addIcon(url:String):void 
		{
			//TmpModel.debugOutput.add("ButtonBase addIcon:" + url);
				
			if (needicon) {
				addChild(icon_up);
				noicontxt.embedFonts = true;
				noicontxt.defaultTextFormat = TmpModel.getTextFormat(18, 0x676767);
				noicontxt.multiline = true;
				noicontxt.width = 170;
				noicontxt.x = 20;
				noicontxt.y = 20;
				noicontxt.selectable = false;
				addChild(noicontxt);
				addChild(icon_down);
				
				needicon = false;
				
				caption_txt = new TextField();
				caption_txt.embedFonts = true;
				caption_txt.defaultTextFormat = TmpModel.getTextFormat(18, 0x676767);
				caption_txt.x = 10;
				caption_txt.y = 100;
				caption_txt.width = 170;
				caption_txt.selectable = false;
				caption_txt.autoSize = TextFieldAutoSize.CENTER;
				caption_txt.text = "";
				addChild(caption_txt);
			}
			caption_txt.text = "";
			noicontxt.visible = false;
			icon_url = url;
			var req:URLRequest = new URLRequest(icon_url);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onIconLoadComplite);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIconLoadError);
			loader.load(req);
		}
		
		private var noIcon:Boolean = true;
		public var noicontxt:TextField = new TextField();
		public var needCaption:String = "";
		public var noPictureFound:Boolean = false;
		
		private function onIconLoadError(e:IOErrorEvent):void 
		{
			noIcon = true;
			//(getChildByName("iconup") as Bitmap).visible = false;
			noicontxt.visible = true;
			noicontxt.text = provider_name;
			visible = false;
			noPictureFound = true;
		}
		public var test_old_icon:Boolean = false;
		private function onIconLoadComplite(e:Event):void 
		{
			noIcon = false;
			(getChildByName("iconup") as Bitmap).bitmapData = Bitmap(Loader(e.target.loader).content).bitmapData;
			image.bitmapData = (getChildByName("iconup") as Bitmap).bitmapData;
			(getChildByName("iconup") as Bitmap).smoothing = true;
			getChildByName("iconup").visible = true;
			var url:String = icon_url.substr(0, icon_url.length - 4) + "_down.png";
			if (test_old_icon) url = url.substr(0, url.length - 9) + ".png";
			var req:URLRequest = new URLRequest(url);
			var loader:Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onIconDownLoadComplite);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onIconDownLoadError);
			loader.load(req);
		}

		private function onIconDownLoadError(e:IOErrorEvent):void 
		{
			(getChildByName("icondown") as Bitmap).bitmapData = (getChildByName("iconup") as Bitmap).bitmapData
			getChildByName("icondown").alpha = 0.6;
			(getChildByName("icondown") as Bitmap).smoothing = true;
			(getChildByName("icondown") as Bitmap).visible = false;
			if (test_old_icon) {
				(getChildByName("icondown") as Bitmap).x = 20;
				(getChildByName("iconup") as Bitmap).y = 20;
				(getChildByName("icondown") as Bitmap).y = 20;
				(getChildByName("iconup") as Bitmap).x = 20;
			}
			caption_txt.text = needCaption;
		}
		
		private function onIconDownLoadComplite(e:Event):void 
		{
			(getChildByName("icondown") as Bitmap).bitmapData = Bitmap(Loader(e.target.loader).content).bitmapData;
			(getChildByName("icondown") as Bitmap).visible = false;
			(getChildByName("icondown") as Bitmap).smoothing = true;
			if (test_old_icon) {
				(getChildByName("icondown") as Bitmap).x = 20;
				(getChildByName("iconup") as Bitmap).y = 20;
				(getChildByName("icondown") as Bitmap).y = 20;
				(getChildByName("iconup") as Bitmap).x = 20;
			}
			caption_txt.text = needCaption;
		}
		
		private function switchIconToDown(sw:Boolean):void { 
			if (icon_url != null && !noIcon) { 
				(getChildByName("icondown") as Bitmap).visible = sw;
				(getChildByName("iconup") as Bitmap).visible = !sw;
			}
			
		}
		
		public function changeDephts():void {
			setChildIndex(image_up, this.numChildren - 1);
			setChildIndex(image_down, this.numChildren - 1);
			setChildIndex(getChildByName("icondown"), this.numChildren - 1);
			setChildIndex(getChildByName("iconup"), this.numChildren - 1);
			setChildIndex(caption_txt, this.numChildren - 1);

		}
	}

}